/**
 *   类说明
 * 
 *   @creator         xldc	497937995@qq.com  
 *   @create-time     2014年1月11日   下午12:36:02   
 */
package com.huacheng.cms.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.huacheng.cms.constant.ConfigConstant;
import com.huacheng.cms.constant.SystemConstant;
import com.huacheng.cms.model.Admin;
import com.huacheng.cms.service.ConfigService;
import com.huacheng.cms.util.HttpUtils;

public class GlobalInterceptor implements HandlerInterceptor {

	@Autowired
	private ConfigService configService;

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (null == modelAndView) {
			return;
		}

		Admin admin = (Admin) request.getSession().getAttribute(SystemConstant.SESSION_ADMIN);
		if (admin == null) {
			modelAndView.addObject("isAdmin", false);
		} else {
			modelAndView.addObject("isAdmin", true);
		}

		// 系统配置参数
		String basePath = HttpUtils.getBasePath(request);

		modelAndView.addObject("basePath", basePath);
		modelAndView.addObject("SYS_SITENAME", configService.getOneByKey(ConfigConstant.SYS_SITENAME));
		modelAndView.addObject("SYS_TEMPLATE", configService.getOneByKey(ConfigConstant.SYS_TEMPLATE));
		modelAndView.addObject("TEMPLATE_PATH",
				basePath + "/themes/" + configService.getOneByKey(ConfigConstant.SYS_TEMPLATE));
		MDC.put("ip", HttpUtils.getIp(request));

		if (StringUtils.isBlank(SystemConstant.BASE_PATH)) {
			SystemConstant.BASE_PATH = basePath;
		}
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

}
